package com.castle.fortress.admin.pay.service.impl;


import com.castle.fortress.admin.pay.dto.*;
import com.castle.fortress.admin.pay.enums.PayPlatformEnum;
import com.castle.fortress.admin.pay.enums.PayTypeEnum;
import com.castle.fortress.admin.pay.service.PayAliConfigService;
import com.castle.fortress.admin.pay.service.PayService;
import com.castle.fortress.admin.pay.service.PayWxConfigService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.BizErrorCode;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author castle
 */
@Service
public class PayServiceImpl implements PayService {
    @Autowired
    private PayWxConfigService payWxConfigService;
    @Autowired
    private PayAliConfigService payAliConfigService;


    @Override
    public RespBody<PayResponse> uniorder(PayRequest request) {
        if(CommonUtil.verifyParamNull(request)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        //微信支付
        if (PayPlatformEnum.WX == request.getPayType().getPlatform()) {
            PayWxConfigDto configDto = payWxConfigService.getPayConfig(request.getPayType().getCode(),request.getSceneCode());
            if (configDto == null) {
                throw new BizException(BizErrorCode.PAY_CONFIG_ERROR);
            }
            WxPayClient wxPayClient = new WxPayClient(configDto);
            return wxPayClient.uniorder(request);
        }
        // 支付宝支付
        else if (PayPlatformEnum.ALIPAY == request.getPayType().getPlatform()) {
            PayAliConfigDto configDto=payAliConfigService.getPayConfig(request.getPayType().getCode(),request.getSceneCode());
            if (configDto == null) {
                throw new BizException(BizErrorCode.PAY_CONFIG_ERROR);
            }
            AliPayClient aliPayClient=new AliPayClient(configDto);
            return aliPayClient.uniorder(request);
        }
        return RespBody.fail(BizErrorCode.PAY_TYPE_ERROR);
    }


    @Override
    public RespBody<RefundResponse> refund(RefundRequest request) {
        //微信退款
        if (PayPlatformEnum.WX == request.getPayType().getPlatform()) {
            PayWxConfigDto configDto = payWxConfigService.getPayConfig(request.getPayType().getCode(),request.getSceneCode());
            if (configDto == null) {
                throw new BizException(BizErrorCode.PAY_CONFIG_ERROR);
            }
            WxPayClient wxPayClient = new WxPayClient(configDto);
            return wxPayClient.refund(request);
        //支付宝退款
        } else if (PayPlatformEnum.ALIPAY == request.getPayType().getPlatform()) {
            PayAliConfigDto configDto=payAliConfigService.getPayConfig(request.getPayType().getCode(),request.getSceneCode());
            if (configDto == null) {
                throw new BizException(BizErrorCode.PAY_CONFIG_ERROR);
            }
            AliPayClient aliPayClient=new AliPayClient(configDto);
            return aliPayClient.refund(request);
        }
        return RespBody.fail(BizErrorCode.PAY_TYPE_ERROR);
    }

    @Override
    public RespBody<PayResponse> asyncNotify(HttpServletRequest request, String notifyData, PayTypeEnum payType, String sceneCode) {
        //微信退款
        if (PayPlatformEnum.WX == payType.getPlatform()) {
            PayWxConfigDto configDto = payWxConfigService.getPayConfig(payType.getCode(),sceneCode);
            if (configDto == null) {
                throw new BizException(BizErrorCode.PAY_CONFIG_ERROR);
            }
            WxPayClient wxPayClient = new WxPayClient(configDto);
            return wxPayClient.asyncNotify(request,notifyData,payType,sceneCode);
            //支付宝退款
        } else if (PayPlatformEnum.ALIPAY == payType.getPlatform()) {
            PayAliConfigDto configDto=payAliConfigService.getPayConfig(payType.getCode(),sceneCode);
            if (configDto == null) {
                throw new BizException(BizErrorCode.PAY_CONFIG_ERROR);
            }
            AliPayClient aliPayClient=new AliPayClient(configDto);
            return aliPayClient.asyncNotify(request,notifyData,payType,sceneCode);
        }
        return RespBody.fail(BizErrorCode.PAY_TYPE_ERROR);
    }


}